package com.xiaoma.tpo.dao;

import java.lang.reflect.Array;
import java.util.Collections;
import java.util.List;

import junit.runner.Version;

import org.springframework.stereotype.Repository;

import com.xiaoma.tpo.baseDao.BaseDaoImpl;
import com.xiaoma.tpo.domain.VVersion;
import com.xiaoma.tpo.dto.PhyVersionVO;
import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.verbal.baseDao.DefaultRowMapper;

@Repository
public class VVersionDao extends BaseDaoImpl<VVersion>{

	public Pager listVersion(String day,Integer page ,Integer rows){
			Pager pager = new Pager();
			Integer startNum;
			
			if(page == null || page == 0){
				startNum = 0;
				
			}else{
				startNum = (page-1)*rows;
				pager.setCurPage(page);
			}
			if(rows == null ){
				rows = pager.getItemsPerPage();
			}else{
				pager.setItemsPerPage(rows);
			}
			if(day != null && day != ""){
				
				String sqlCount = " select count(vv.id) "
						+ " from v_version vv , v_physical_address vp "
						+ " where vv.platform_guid = vp.guid "
						+ " and vv.update_date > '"+day+" 00:00:00' and vv.update_date < '"+day+" 23:59:59' ";
				String sqlList = "select vv.id , vv.version , vv.description , vv.is_latest , vv.download_url , vv.platform_guid,vv.is_force_update,"
						+ " vv.create_date , vv.update_date,  vp.id as guid, vp.name "
						+ " from v_version vv , v_physical_address vp"
						+ " where vv.platform_guid = vp.guid "
						+ " and  vv.update_date > '"+day+" 00:00:00' and vv.update_date < '"+day+" 23:59:59' ";
//				sqlCount = sqlCount  + " limit "+startNum+","+rows ;
				sqlList  = sqlList  + " order by vv.platform_guid, vv.version  limit "+startNum+","+rows ;
				Integer totalCount = jdbcTemplate.queryForInt(sqlCount);
				List<PhyVersionVO> phyVersionVOs = jdbcTemplate.query(sqlList, new DefaultRowMapper(PhyVersionVO.class, this.getNameHandler()));
				pager.setList(phyVersionVOs);
				pager.setTotalCount(totalCount);
				
				
			}else{
				String sqlCount = " select count(vv.id) "
						+ " from v_version vv , v_physical_address vp"
						+ " where vv.platform_guid = vp.guid ";
				Integer totalCount = jdbcTemplate.queryForInt(sqlCount);
				String sqlList = "select vv.id , vv.version ,vv.description ,  vv.version , vv.is_latest , vv.download_url , vv.platform_guid,vv.is_force_update,"
						+ " vv.create_date , vv.update_date,  vp.id as guid, vp.name "
						+ " from v_version vv , v_physical_address vp"
						+ " where vv.platform_guid = vp.guid ";
//				sqlCount = sqlCount  + " limit "+startNum+","+rows ;
				sqlList  = sqlList  + " order by vv.platform_guid, vv.version  limit "+startNum+","+rows ;
				List<PhyVersionVO> phyVersionVOs = jdbcTemplate.query(sqlList, new DefaultRowMapper(PhyVersionVO.class, this.getNameHandler()));
				pager.setList(phyVersionVOs);
				pager.setTotalCount(totalCount);
				
			}
	return pager;
	}

	public List<PhyVersionVO> getNewVersion(String guid,String version) {
		String sqlIsLatest = "select vv.is_latest "
				+ " from v_version vv , v_physical_address vp"
				+ " where vv.platform_guid = vp.guid and vv.version = '"+version+"' and vp.guid = '"+guid+"'";
		List<PhyVersionVO> phyVersionVOs = jdbcTemplate.query(sqlIsLatest, new DefaultRowMapper(PhyVersionVO.class, this.getNameHandler()));
		List<PhyVersionVO> rPhyVersionVOs = null;
		String isLatest = "";
		if(phyVersionVOs != null && phyVersionVOs.size()>0){
			isLatest = phyVersionVOs.get(0).getIsLatest();
		}else{
			String sqlMaxVersion = "select vv.version "
					+ " from v_version vv , v_physical_address vp"
					+ " where vv.platform_guid = vp.guid and vv.is_latest='1' and vp.guid = '"+guid+"'";
			List<PhyVersionVO> lphyVersionVOs = jdbcTemplate.query(sqlMaxVersion, new DefaultRowMapper(PhyVersionVO.class, this.getNameHandler()));
			String maxVersion = lphyVersionVOs.get(0).getVersion();
			int b = version.compareTo(maxVersion);
			if(b>0){
				isLatest = "1";
			}
			if(b<0){
				isLatest = "0";
			}
		}
		if("0".equals(isLatest)){
			String sqlList = "select vv.id , vv.version ,vv.description , vv.is_latest , vv.download_url , vv.platform_guid,"
					+ " vv.create_date , vv.update_date,  vp.id , vp.guid, vp.name "
					+ " from v_version vv , v_physical_address vp"
					+ " where vv.platform_guid = vp.guid and vv.is_latest = '1' and vp.guid = '"+guid+"'";
			rPhyVersionVOs = jdbcTemplate.query(sqlList, new DefaultRowMapper(PhyVersionVO.class, this.getNameHandler()));
			String isLatestSql = "select  vv.is_force_update"
					+ " from v_version vv , v_physical_address vp"
					+ " where vv.platform_guid = vp.guid and vv.version = '"+version+"' and vp.guid = '"+guid+"'";
			List<PhyVersionVO> rrPhyVersionVOs = jdbcTemplate.query(isLatestSql, new DefaultRowMapper(PhyVersionVO.class, this.getNameHandler()));
			if(rrPhyVersionVOs != null && rrPhyVersionVOs.size() > 0){
				if(rPhyVersionVOs != null && rPhyVersionVOs.size()>0){
					rPhyVersionVOs.get(0).setIsForceUpdate(rrPhyVersionVOs.get(0).getIsForceUpdate());
				}
			}else{
				if(rPhyVersionVOs != null && rPhyVersionVOs.size() >0){
					rPhyVersionVOs.get(0).setIsForceUpdate("0");
				}
			}
			
		}
		return rPhyVersionVOs;
	}

	public List<VVersion> getVersionByIsLatestAndGuid(VVersion vVersion) {
		String sqlList = "select id , version ,description , is_latest , download_url , platform_guid ,"
				+ " create_date , update_date "
				+ " from v_version  "
				+ " where  is_latest = '1' and  platform_guid = '"+vVersion.getPlatformGuid()+"'";
		List<VVersion> vVersions = jdbcTemplate.query(sqlList, new DefaultRowMapper(VVersion.class, this.getNameHandler()));
		
		return vVersions;
	}

	public VVersion getLastLatestVersion(VVersion vVersion) {
		String sql = "select * from v_version where is_latest = '0' and platform_guid='"+vVersion.getPlatformGuid()+"'";
		List<VVersion> lVersions = jdbcTemplate.query(sql, new DefaultRowMapper(VVersion.class, this.getNameHandler()));
		if(lVersions !=null && lVersions.size() >0){
			Collections.sort(lVersions);
			lVersions.get(0).setIsLatest("1");
			lVersions.get(0).setIsForceUpdate("0");
		}
		return lVersions.get(0);
	}

	public List<PhyVersionVO> getNewVersion(String guid) {
		String sqlList = "select vv.id , vv.version ,vv.description , vv.is_latest , vv.download_url , vv.platform_guid,vv.is_force_update,"
				+ " vv.create_date , vv.update_date,  vp.id , vp.guid, vp.name "
				+ " from v_version vv , v_physical_address vp"
				+ " where vv.platform_guid = vp.guid and vv.is_latest = '1' and vp.guid = '"+guid+"'";
		List<PhyVersionVO> rPhyVersionVOs = jdbcTemplate.query(sqlList, new DefaultRowMapper(PhyVersionVO.class, this.getNameHandler()));
		return rPhyVersionVOs;
	}

	public boolean isMostVersion(VVersion vVersion) {
		String mostVersin = "select * from v_version where version > '"+vVersion.getVersion()+"'";
		List<VVersion> vVersions = jdbcTemplate.query(mostVersin, new DefaultRowMapper(VVersion.class, this.getNameHandler()));
		if(vVersions != null && vVersions.size() > 0){
			return false;
		}
		return true;
	}
}

